package com.axun.ckz.yytmanager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.axun.ckz.yytmanager.R;
import com.axun.ckz.yytmanager.app.MyApp;
import com.axun.ckz.yytmanager.base.BaseActivity;
import com.axun.ckz.yytmanager.presenter.LoginPresenter;
import com.axun.ckz.yytmanager.response.LoginResponseBean;
import com.axun.ckz.yytmanager.util.SBTools;
import com.axun.ckz.yytmanager.util.StatusBarUtil;
import com.axun.ckz.yytmanager.util.StatutBarTools;
import com.axun.ckz.yytmanager.view.LoginView;
import com.igexin.sdk.PushManager;

public class LoginActivity extends BaseActivity implements LoginView{

    private LoginPresenter presenter;

    private LinearLayout mParentView;
    private LinearLayout mLoginLoginLayout;
    private EditText mLoginUserCode;
    private EditText mLoginUserEt;
    private EditText mLoginPasswordEt;
    private Button mLoginLoginBtn;
    private TextView mTvServerName;
    private TextView mTvVersionName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SBTools.setChengjin(this);
        setContentView(R.layout.activity_login);
        presenter = new LoginPresenter(getContext(),this);
        initView();
        presenter.getUrl(mLoginUserCode.getText().toString());
    }

    private void initView() {
        mParentView = (LinearLayout) findViewById(R.id.parent_view);
        mLoginLoginLayout = (LinearLayout) findViewById(R.id.login_login_layout);
        mLoginUserCode = (EditText) findViewById(R.id.login_userCode);
        mLoginUserEt = (EditText) findViewById(R.id.login_userEt);
        mLoginPasswordEt = (EditText) findViewById(R.id.login_passwordEt);
        mLoginLoginBtn = (Button) findViewById(R.id.login_login_btn);
        mTvServerName = (TextView) findViewById(R.id.tv_server_name);
        mTvVersionName = (TextView) findViewById(R.id.tv_version_name);
        mLoginUserCode.setText(MyApp.code);
        initTextChange();
        mLoginLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String client = PushManager.getInstance().getClientid(getContext());
                presenter.login(mLoginUserEt.getText().toString(),mLoginPasswordEt.getText().toString(),client);
            }
        });
    }

    private void initTextChange(){
        mLoginUserCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                presenter.getUrl(s.toString());
            }
        });
    }

    @Override
    public void showService(String name) {
        mTvServerName.setText(name);
    }

    @Override
    public void showVersion(String version) {
        mTvVersionName.setText("v"+version);
    }

    @Override
    public void loginSuccess(LoginResponseBean bean) {
        Intent intent = new Intent(getContext(),MainActivity.class);
        intent.putExtra("remark",bean.getRemark());
        startActivity(intent);
        finish();
    }

    @Override
    public void loginFaild() {

    }
}
